package com.example.interactivehouse;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class LightIndoorsScreen extends Activity {
	Socket socket = null;
	DataOutputStream out = null;
	DataInputStream in = null;
	String status = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lightindoors);
		final Button light = (Button) findViewById(R.id.lightindoors);
		final Button home = (Button) findViewById(R.id.homel);
		final Button on = (Button) findViewById(R.id.indoorlon);
		final Button off = (Button) findViewById(R.id.indoorloff);
		status = "light:on";
//		light.setOnClickListener(new OnClickListener() {
//			public void onClick(View v) {
//				establishContact();
//				String get = null;
//				try {
//					if (status.equalsIgnoreCase("light:off")) {
//						out.writeUTF(status);
//						light.setBackgroundResource(R.drawable.lighton);
//						get = in.readUTF();
//						System.out.println("the light now is:" + get);
//						status = "light:on";
//					} else if (status.equals("light:on")) {
//						out.writeUTF(status);
//						light.setBackgroundResource(R.drawable.lightoff);
//						get = in.readUTF();
//						System.out.println("the light now is:" + get);
//						status = "light:off";
//					}
//					// System.out.println("get request from client: ");
//				} catch (IOException ioe) {
//					ioe.printStackTrace();
//				}
//				closeContact();
//			}
//		});
		on.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
//				establishContact();
//				String get = null;
//				try {
//					String on = "light:on";
//					out.writeUTF(on);
//					get = in.readUTF();
//					if (get.equalsIgnoreCase(on)) {
						light.setBackgroundResource(R.drawable.lighton);
//					}
//					System.out.println("the light now is:" + get);
//				} catch (IOException ioe) {
//					ioe.printStackTrace();
//				}
//				closeContact();
			}
		});
		off.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
//				establishContact();
//				String get = null;
//				try {
//					String off = "light:off";
//					out.writeUTF(off);
//					get = in.readUTF();
//					if (get.equalsIgnoreCase(off)) {
						light.setBackgroundResource(R.drawable.lightoff);
//					}
//					System.out.println("the light now is:" + get);
//				} catch (IOException ioe) {
//					ioe.printStackTrace();
//				}
//				closeContact();
			}
		});
		home.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				home.setBackgroundResource(R.drawable.hometap);
				Intent i = new Intent(LightIndoorsScreen.this,
						InteraciveHouse.class);
				startActivity(i);
			}
		});
	}

	private void establishContact() {
		try {
			// socket = new Socket("194.47.46.78", 12345);
			socket = new Socket("192.168.1.102", 12345);
			out = new DataOutputStream(socket.getOutputStream());
			in = new DataInputStream(socket.getInputStream());
		} catch (UnknownHostException e) {
			System.err.println("Don't know about host.");
			System.exit(1);
		} catch (IOException e) {
			System.err.println("Couldn't get I/O");
			System.exit(1);
		}
	}

	private void closeContact() {
		try {
			out.close();
			in.close();
			socket.close();
		} catch (IOException ioe) {
			System.out.println("Failed");
			System.exit(-1);
		}
	}
}
